package com.nbjtjc.safe.api.emg;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgPlanGoodsMapper;
import com.nbjtjc.safe.model.emg.EmgPlanGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 应急物资
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/planGoods")
public class EmgPlanGoodsController extends AbstractController<EmgPlanGoods> {

    @Autowired
    private EmgPlanGoodsMapper emgPlanGoodsMapper;

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @Override
    @SysLog("删除应急预案中绑定的物资")
    public Result delete(int id) {
        Result result=new Result();
        if(id<=0){
            result.setFalse("id不能小于0");
            return result;
        }
        try {
            emgPlanGoodsMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find",method = RequestMethod.POST)
    @Override
    public Result find(EmgPlanGoods param, Page page) {
        Result result=new Result();
        if(ObjectUtil.isNull(param)||ObjectUtil.isNull(param.getPlanId())){
            result.setFalse("不能为空");
            return result;
        }
        try {
            PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
            List<EmgPlanGoods> emgPlanGoods=emgPlanGoodsMapper.findByPlanId(param.getPlanId());
            result.setData(new PageInfo<>(emgPlanGoods));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SysLog("添加应急预案中绑定的物资")
    public Result add(EmgPlanGoods emgPlanGoods) {
        Result result=new Result();
        if(ObjectUtil.isNull(emgPlanGoods)||ObjectUtil.isNull(emgPlanGoods.getPlanId())
                ||ObjectUtil.isNull(emgPlanGoods.getName())){
            result.setFalse("不能为空");
            return result;
        }
        try {
            emgPlanGoodsMapper.insert(emgPlanGoods);
            result.setData(emgPlanGoods);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

}
